package com.uniledger.exchange.model.rx;

import android.app.Activity;
import android.support.annotation.NonNull;

import com.orhanobut.logger.Logger;
import com.uniledger.exchange.R;
import com.xiasuhuei321.loadingdialog.view.LoadingDialog;

import org.reactivestreams.Publisher;
import org.reactivestreams.Subscription;

import java.lang.ref.WeakReference;

import io.reactivex.Flowable;
import io.reactivex.FlowableTransformer;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

public class RxProgress {

    public static <T> FlowableTransformer<T, T> applyProgressBar(
            @NonNull final Activity activity, String msg) {
        final WeakReference<Activity> activityWeakReference = new WeakReference<>(activity);
        LoadingDialog dialog = new LoadingDialog(activityWeakReference.get());
        dialog.setLoadStyle(LoadingDialog.STYLE_LINE);
        dialog.setLoadingText(activity.getResources().getString(R.string.loading));
        dialog.closeFailedAnim();
        dialog.closeSuccessAnim();
        return new FlowableTransformer<T, T>() {
            @Override
            public Publisher<T> apply(Flowable<T> upstream) {
                return upstream
                        .doOnSubscribe(new Consumer<Subscription>() {
                            @Override
                            public void accept(Subscription subscription) throws Exception {
//                                Logger.d("doOnSubscribe");
                                dialog.show();
                            }
                        })
                        .doOnComplete(new Action() {
                            @Override
                            public void run() throws Exception {
//                                Logger.d("doOnComplete");
                                dialog.loadSuccess();
                            }
                        })
                        .doOnError(new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
//                                Logger.d("doOnError");
                                dialog.setFailedText(throwable.getMessage());
                                dialog.loadFailed();
                            }
                        })
                        .doOnTerminate(new Action() {
                            @Override
                            public void run() throws Exception {
//                                Logger.d("doOnTerminate");
//                                dialog.close();
                            }
                        })
                        .doOnCancel(new Action() {
                            @Override
                            public void run() throws Exception {
//                                Logger.d("doOnCancel");
//                                dialog.close();
                            }
                        })
                        .doFinally(new Action() {
                            @Override
                            public void run() throws Exception {
//                                Logger.d("doFinally");
                                dialog.close();
                            }
                        });
            }
        };
    }

    public static <T> FlowableTransformer<T, T> applyProgressBar(
            @NonNull final Activity activity) {
        return applyProgressBar(activity, "");
    }
}